package com.itheima.Interceptor;

import com.itheima.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response ,Object handler) throws Exception {

        //1、获取请求头令牌
        String token = request.getHeader("token");


        //2、令牌校验(令牌为空，或者令牌解析不通过，都要拦截)
        if(ObjectUtils.isEmpty(token) || JwtUtils.paraseJwt(token)==null){
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return false;
        }





        //3、校验通过，放行请求
        return true;

    }




}


